package com.test.springsecurity.service.impl;

import com.test.springsecurity.entity.AppUser;
import com.test.springsecurity.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户信息的service实现类，需要实现接口UserDetailsService
 *
 * springsecurity在进行认证（登录）时会自动调用该实现类的方法
 */
@Service
@Slf4j
public class AppUserDetailServiceImpl implements UserDetailsService {
    //注入参数
    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //
        AppUser appUser =userMapper.findByUsername(username);
        //判断用户是否存在
        if(appUser==null)   throw new UsernameNotFoundException("账号不存在");

        log.info(appUser.toString());
        return appUser;
    }
}
